package com.efrei.schoolmanager.tools;

import com.efrei.schoolmanager.exceptions.LogicException;

//import org.jdom.input.SAXBuilder;


public class Verifs {
	
	/**
	 * Verification des emails
	 * @param email
	 * @return
	 */
	public static boolean isValidMail(String email)
	{
		String emailReg = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$";
		return email.matches(emailReg);
	}
	
	/**
	 * Verification du num�ro de t�l�phone entre par l'utilisateur
	 * @param telephone
	 * @return
	 */
	public static boolean isValidPhoneNumber(String telephone)
	{
		String number="^[0-9]{10,10}$";
		return telephone.matches(number);
	}

	/**
	 * @param value
	 * @throws LogicException
	 * @see {@link #checkString(String, String)}
	 */
	public static String checkString(String value) throws LogicException {
		return checkString(value, "Chaine invalide");
	}
	
	/**
	 * verifie si value !=null && != ""
	 * sinon lance une exception.
	 * @param value
	 * @param errorMessage message de l'exception si erreur
	 * @return
	 * @throws LogicException
	 */
	public static String checkString(String value, String errorMessage) throws LogicException {
		if(value==null || value.length()==0)
			throw new LogicException(errorMessage);
		return value;
	}
	
	/**
	 * verifie si l'objet est null
	 * @param value
	 * @param errorMessage
	 * @return
	 * @throws LogicException
	 */
	public static String checkNullString(String value, String errorMessage) throws LogicException {
		if(value==null)
			throw new LogicException(errorMessage);
		return value;
	}

	
	public static String checkSexe(String sexe) throws LogicException {
		if(checkString(sexe, "Sexe invalide").equals("f") ||
			sexe.equals("h")) {
			return sexe;
		}
		throw new LogicException("Sexe invalide");
	}
}
